<template>
  <div v-loading.lock="loading" class="base-content">
    <div class="basic-info">
      <el-form ref="queryForm" :inline="true" :model="queryForm">
        <el-form-item label="编号" prop="num">
          <el-input
          v-model="queryForm.num"
          placeholder="请输入编号"
          clearable
          />
        </el-form-item>
        <el-form-item label="项目名称" prop="detectionItemName">
          <el-input
            v-model="queryForm.detectionItemName"
            placeholder="请输入项目名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="标准名称" prop="methodStandardName">
          <el-input
            v-model="queryForm.methodStandardName"
            placeholder="请输入标准名称"
            clearable
          />
        </el-form-item>
         <el-form-item class="base-filter-button">
          <el-button
            type="primary"
            @click="onQuery"
            >查询</el-button>
          <el-button @click="cancelForm">清空</el-button>
        </el-form-item>
      </el-form>
      <div class="base-content-operate">
        <el-button
          type="primary"
          icon="el-icon-setting"
          @click.native="beatchUploadPrice()"
          >设置价格</el-button>
        <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="exportData()"
          >导出</el-button
        >
        <el-button
          icon="el-icon-document-copy"
          type="primary"
          @click="importData"
          >导入</el-button>
      </div>
      <el-table
        ref="table"
        :data="list"
        tooltip-effect="dark"
        height="calc(100% - 150px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column align="center" label="序号" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column v-if="false" label="ID">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>
        <el-table-column label="编号">
          <template slot-scope="scope">
            {{ scope.row.num }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称">
          <template slot-scope="scope">
            {{ scope.row.detectionItemName }}
          </template>
        </el-table-column>
        <el-table-column label="标准名称">
          <template slot-scope="scope">
            {{ scope.row.methodStandardName }}
          </template>
        </el-table-column>
          <el-table-column label="单价(元)">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.agreementPrice"
              size="mini"
              placeholder="请输入价格"
            >
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="协议价1(元)">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.firstAgreementPrice"
              size="mini"
              placeholder="请输入价格"
            >
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="协议价2(元)">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.secondAgreementPrice"
              size="mini"
              placeholder="请输入价格"
            >
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="协议价3(元)">
          <template slot-scope="scope">
            <el-input
              v-model="scope.row.thirdAgreementPrice"
              size="mini"
              placeholder="请输入价格"
            >
            </el-input>
          </template>
        </el-table-column>
        <el-table-column label="备注/说明">
          <template slot-scope="scope">
            {{ scope.row.explain }}
          </template>
        </el-table-column>
      </el-table>
      <BasePagination
        :total="pagination.total"
        :page-index="pagination.pageIndex"
        :page-size="pagination.pageSize"
        @sizeChange="sizeChange"
        @currentChange="currentChange"
      />
      <DetectionItemBeatchUpdateDialog
        v-if="dialogVisible"
        :dialog-visible.sync="dialogVisible"
        :top-text="topText"
        :detection-item-id-array="detectionItemIdArray"
        @DetectionItemClose="DetectionItemClose"
        @getList="getList"
      />
    </div>
  </div>
</template>
<script>
import {
  getList,
  UpdatePrice,
  getExportList,
  DownloadEntrustOrderMaggess,
} from "../../../api/SysBusiness/SysBusinessDetectionItem.js";
import DetectionItemBeatchUpdateDialog from "./DetectionItemBeatchUpdateDialog.vue";

export default {
  name: "EntrustProjectPriceList",
  components: { DetectionItemBeatchUpdateDialog },
  data() {
    return {
      dialogVisible: false,
      loading: true,
      topText: "",
      idSendArry: [],
      list: [],
      queryForm: {
        num: "",
        detectionItemName: "",
        methodStandardName: "",
      },
      formData: {
        id: 0,
        parentId: 0,
        productStandardId: 0,
        sysBusinessSampleCategoryId: 0,
        num: "",
        detectionItemName: "",
        anotherName: "",
        detectionObject: "",
        parameterName: "",
        standardMethod: "",
        detectionMethod: "",
        restrictRange: "",
        explain: "",
        detectionItemType: "",
        restrict: "",
        firstAgreementPrice: 0,
        secondAgreementPrice: 0,
        thirdAgreementPrice: 0,
        unitSymbol: "",
        decisionSymbol: "",
        qualityIndex: "",
        methodStandard: "",
        methodStandardName: "",
        AgreementPrice: "",
        AgreementPriceDifferentiate: "",
      },
      pagination: {
        pageIndex: 1,
        pageSize: 10,
        total: 0,
      },
      detectionItemIdArray: [],
      multipleSelection: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    currentChange(val) {
      this.pagination.pageIndex = val;
      this.getList();
    },
    sizeChange(val) {
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      getList(this.queryForm, this.pagination)
        .then((res) => {
          this.list = res.data.dataList ? res.data.dataList : [];
          this.list.forEach((item) => {
            item.firstAgreementPrice = item.firstAgreementPrice / 100;
            item.secondAgreementPrice = item.secondAgreementPrice / 100;
            item.thirdAgreementPrice = item.thirdAgreementPrice / 100;
            item.agreementPrice = item.agreementPrice / 100;
          });
          this.pagination.total = res.data.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    onQuery() {
      this.getList();
    },
    // 清空筛选条件
    cancelForm() {
      this.$refs.queryForm.resetFields();
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    beatchUploadPrice(data) {
      if (this.multipleSelection.length > 0) {
        this.detectionItemIdArray = [];
        this.multipleSelection.forEach((item) => {
          this.detectionItemIdArray.push(item.id);
        });
        this.dialogVisible = true;
      } else {
        this.$message.warning("请选择检测项目");
      }
    },
    outPutQuery() {},

    DetectionItemClose(data) {},
    // 导出
    exportData() {
      getExportList(this.queryForm).then((res) => {
        DownloadEntrustOrderMaggess().then((res) => {
          const link = document.createElement("a");
          const blob = new Blob([res.data], {
            type: "application/vnd.ms-excel",
          }); // 文件流处理
          link.style.display = "none"; // 去除a标签的样式
          // 设置连接
          link.href = URL.createObjectURL(blob);
          link.download = "检测项目报价清单.xlsx";
          document.body.appendChild(link);
          // 模拟点击事件;
          link.click();
        });
      });
    },
    updatePriceFunction(data) {
      var _formData = this.formData;
      _formData.id = data.id;
      _formData.firstAgreementPrice = data.firstAgreementPrice;
      _formData.secondAgreementPrice = data.secondAgreementPrice;
      _formData.thirdAgreementPrice = data.thirdAgreementPrice;
      if (
        _formData.firstAgreementPrice === "" ||
        _formData.secondAgreementPrice === "" ||
        _formData.thirdAgreementPrice === ""
      ) {
        this.$message.warning("价格不能为空");
        return;
      }
      this.$confirm("此操作将设置服务金额, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        callback: (action) => {
          if (action === "confirm") {
            UpdatePrice(_formData)
              .then((res) => {
                getList();
              })
              .catch((res) => {});
          }
        },
      });
    },
  },
};
</script>
